using GraphQL.Types;
using GraphQLDemo.Domain;
using GraphQLDemo.Domain.Companies;
using GraphQLDemo.Domain.OrderAttachments;
using GraphQLDemo.Domain.Orders;

namespace GraphQLDemo.Api.GroupTypes;

public class OrderGraphType : ObjectGraphType<Order>
{
    public OrderGraphType()
    {
        Name = "Order";
        Description = "test...";

        Field(o => o.Price);
        Field(o => o.StatusType);
        Field(o => o.CreationTime);
        Field<ContractGraphType>("contract")
            .Arguments(new QueryArguments()
            {
                new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "id", Description = "id of the human" },
            })
            .Resolve(context =>
            {
                var contractId = context.Source.ContractId;
                var contract = new Contract()
                {
                    Id = contractId,
                };
                return contract;
            })
            .ApplyPortal(this, PortalType.Client);

        Field<CompanyGraphType>("company")
            .Arguments(new QueryArguments()
            {
                new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "id", Description = "id of the human" },
            })
            .Resolve(context =>
            {
                var companyId = context.Source.CompanyId;
                var company = new Company()
                {
                    Id = companyId,
                };
                return company;
            });
        Field<StringGraphType>("number")
            .Resolve(context =>
            {
                context.Source.Number = context.Source.Id.ToString();
                return context.Source.Number;
            });

        Field<ListGraphType<OrderAttachmentGraphType>>("attachments")
            .Resolve(context =>
            {
                var id = context.Source.Id;
                var orderAttachments = new List<OrderAttachment>()
                {
                    new OrderAttachment()
                    {
                        Id = Guid.NewGuid(),
                        OrderId = id,
                    }
                };
                return orderAttachments;
            });
    }
}